package similarity;

import type.User;

public class OtherSimilarity
{
	public static int YOUNG_CHILDREN = 0;
	public static int CHILDREN = 1;
	public static int TEEN = 2;
	public static int YOUNG_ADULTS = 3;
	public static int ADULTS = 4;
	public static int OLD_PEOPLE = 5;
	
	static public double betweenDemographic(User u, User v)
	{
		double retVal = 0.0;
		
		retVal += u.getSex().equals(v.getSex()) ? 1.0 : 0.0;
		retVal += u.getOccupation().equals(v.getOccupation()) ? 1.0 : 0.0;
		retVal += getAgeGroup(u.getAge()) == getAgeGroup(v.getAge()) ?  1.0 : 0.0;
		retVal += getRegionSimilarityFromZipCode(u.getZipCode(), v.getZipCode());
		return retVal / 4.0;
	}
	
	private static double getRegionSimilarityFromZipCode(String aZipCode1, String aZipCode2)
	{
		double retVal = 0.0;
		if(aZipCode1.length() != aZipCode2.length())
		{
			//Different length => different regions => no similarity 
			return 0.0;
		}
		
		for( int i = 0; i < aZipCode1.length(); ++i)
		{
			if(aZipCode1.charAt(i) == aZipCode2.charAt(i))
			{
				retVal += 1.0 / (double)aZipCode1.length();
			}
			else
			{
				break;
			}
		}
		
		return retVal;
	}
	
	private static int getAgeGroup(int aAge)
	{
		if(aAge < 8){ return YOUNG_CHILDREN;	}
		if(aAge < 14){ return CHILDREN; }
		if(aAge < 20){ return TEEN; }
		if(aAge < 30){ return YOUNG_ADULTS; }
		if(aAge < 65){ return ADULTS; }
		return OLD_PEOPLE;
	}
}
